////////////////////////////////////////////////////////////////////////////////
// Master Do-File
// INTERNATIONAL PORTFOLIO CHOICE WITH FRICTIONS: EVIDENCE FROM MUTUAL FUNDS
// by
// Philippe Bacchetta, Simon Tièche, and Eric van Wincoop
// April 2023
////////////////////////////////////////////////////////////////////////////////
clear all
macro drop _all
set more off
program drop _all

*************************************************************************
*** to install following packages, set install == 1
local install = 0
if `install' == 1 {
	ssc install sencode
	ssc install ftools
	ssc install reghdfe
	ssc install ivreghdfe
}
************************************************************************

************************************************************************
// DEFINE USER: set path:

local userdir = 2

if `userdir'== 1 { // user 1
	global path "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

else if `userdir' == 2{ // user 2
	global path "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
************************************************************************

************************************************************************
// SET PATH following folder structure
cd $path
global datapath "$path/data"
global figpath "$path/tables"
global tabpath "$path/figures"

global buildpath "$path/codes/_do_build"
global estimpath "$path/codes/_do_estim"
global simulpath "$path/codes/_do_simul"
***************************************************************************

************************************************************************
*** 1. BUILD EPFR DATA USING THE MONTHLY COUNTRY ALLOCATION AND THE WEEKLY FLOWS
** 1.a Builds Monthly Data from master .txt files
run "$buildpath/__EPFRdata_mallocations_p"

** 1.b Builds Weekly Data from master .txt files
run "$buildpath/__EPFRdata_wflows_p"

** 1.c EPFR  data for main results
run "$buildpath/__EPFRdata__4__Main__Results"
***************************************************************************

***************************************************************************
*** 2. BUILD EQUITY RETURN DATA - please do run the files sequentially as indicated by the numbering

** 2.a Convert MSCI Excel Data in Stata dta format
* BEFORE RUNNING THIS FILE: 
* (I)  DOWNLOAD DATA FROM DATASTREAM USING FILE "Request_Equity_Indices" in data/datastream
* (II) RUN FILE "clean_equity_indices" WITH PYTHON TO CLEAN THE DATA
run "$buildpath/MSCI_exceldata_to_DTA"

** 2.b Convert daily MSCI Excel Data in Stata dta format
* BEFORE RUNNING THIS FILE: 
* (I)  DOWNLOAD DATA FROM DATASTREAM USING FILE "Request_daily_Equity_Indices" in data/datastream
* (II) RUN FILE "clean_daily_equity_indices" WITH PYTHON TO CLEAN THE DATA
run "$buildpath/MSCI_daily_exceldata_to_DTA"

** 2.c present discounted value of equity return based on total return index
run "$buildpath/Prediction_DiscountedReturns"

** 2.d MSCI variables: US DIFFERENTIALS
run "$buildpath/Prediction_Differential"

** 2.e daily equity return based on equity price index
run "$buildpath/MSCI_daily_equity_returns"
************************************************************************

************************************************************************
*** 3. BUILD OTHER DATA
** 3.a bilateral exchange rate with respect to US
* BEFORE RUNNING THIS FILE: DOWNLOAD DATA FROM DATASTREAM USING FILE "Request_Exchange_Rates" in data/datastream
run "$buildpath/Exchange_Rates_exceldata_to_DTA"

** 3.b interest rates
* BEFORE RUNNING THIS FILE: DOWNLOAD DATA FROM DATASTREAM USING FILE "Request_interest_rates (local currency)" in data/datastream and convert interest rate in local currency to USD using the bilateral exchange rates
run "$buildpath/Interest_Rates_exceldata_to_DTA"

** 3.c bond index
run "$buildpath/bonds"

** 3.d consumer, business confidence index and leading indicators
run "$buildpath/confidence"
************************************************************************

************************************************************************
*** 4. PREDICTIONS, BUILD FUND-LEVEL EXPECTED EXCESS RETURNS, INSTRUMENTS, RISK, ETC

** 4.a PREDICTIONS + TABLES 1, 2 IN THE MAIN PAPER AND TABLE A1 IN INTERNET APPENDIX
run "$estimpath/Portfolio_Paper_Predictions"

** 4.b TABLES 3, 4, 6 AND 7 IN MAIN TEXT + TABLES B1, B2, B3, AND B4 + FIGURES C1 
run "$estimpath/MainResults"
************************************************************************

************************************************************************
*** 5. SIMULATIONS

** 5.1 FIGURE 1
run "$simulpath/figure1"

** 5.2 TABLE 5
* USE MATLAB TO RUN THE FILE NAMED "Table5_April_2023" in $simulpath
************************************************************************

************************************************************************
*** 6. TRADING STRATEGIES AND PREDICTABILITY OUT OF SAMPLE
** TABLE C1 IN MAIN PAPER, TRADING STRATEGIES
run "$estimpath/Table_C1_Strategy"

** TABLE A2 IN APPENDIX, PREDICTABILITY OUT OF SAMPLE
run "$estimpath/Portfolio_Paper_PredictabilityOFS"
************************************************************************

************************************************************************
*** 7. COMPARISON WITH BERTAUT AND TRYON AND BERTAUT AND JUDSON

** MERGE BERTAUT AND TRYON (2007) AND BERTAUT AND JUDSON (2014, MOST RECENT DATA) IN A SINGLE .DTA FORMAT. SAVE IT IN $datapath
** THEN RUN THIS FILE:
run "$estimpath/internet_appendix_D1_figures_tables"
************************************************************************